const {
  ipcRenderer
} = require('electron')
const Timer = require('timer.js')

function startWork(sencond, type) {
  let workTimer = new Timer({
    tick: 1,
    ontick: (sec) => {
      updateTime(sec)
    },
    onend: () => {
      notification(type)
    },
    onstart: () => {
      console.log('timer started');
    }
  })
  workTimer.start(sencond)
}

function updateTime(ms) {
  let timerContainer = document.getElementById('timer-container')
  let s = (ms / 1000).toFixed(0)
  let ss = s % 60;
  let mm = (s / 60).toFixed(0);
  timerContainer.innerText = `${mm.toString().padStart(2,0)}:${ss.toString().padStart(2,0)}`
}

async function notification(type) {
  let situationContainer = document.getElementById('situation-container')
  if (type == "work") {
    let res = await ipcRenderer.invoke('work-notification')
    if (res === 'rest') {
      alert('请离开电脑休息')
      situationContainer.innerText = "休息时间"
      
    }
    startWork(11,res)
    
  }
  else if (type == "rest") {
   
    situationContainer.innerText = "工作时间"
    startWork(11,"work")
  }
  
}

startWork(11,"work")